import java.util.StringTokenizer;

class P482 {

   public static void main(String[] args) {
       int cases = Integer.parseInt(readLn());
       for (int k = 0; k < cases; k++) {
               readLn();
               String order = readLn();
               StringTokenizer st = new StringTokenizer(order);
               int[] orderAr = new int[st.countTokens()];
               for (int i = 0; i < orderAr.length; i++) {
                       orderAr[i] = Integer.parseInt(st.nextToken());
               }

               String values = readLn();
               st = new StringTokenizer(values);

               String[] valuesAr = new String[st.countTokens()];
               for (int i = 0; i < valuesAr.length; i++) {
                       valuesAr[i] = st.nextToken();
               }

               process(orderAr, valuesAr);
               if (k + 1 != cases) System.out.println();
       }
   }

       private static void process(int[] orderAr, String[] valuesAr) {

               for (int k = 0; k < orderAr.length; k++) {
                       for (int j = k + 1; j < orderAr.length; j++) {
                               if (orderAr[k] > orderAr[j]) {
                                       swap(orderAr, k, j);
                                       swap(valuesAr, k, j);
                               }
                       }
               }

               for (int k = 0; k < valuesAr.length; k++) {
                       System.out.println(valuesAr[k]);
               }
       }

       private static void swap(String[] array, int k, int j) {
               String aux = array[k];
               array[k] = array[j];
               array[j] = aux;
       }

       private static void swap(int[] array, int k, int j) {
               int aux = array[k];
               array[k] = array[j];
               array[j] = aux;

       }

       static String readLn() {
       String newLine = System.getProperty("line.separator");
       StringBuffer buffer = new StringBuffer();
       int car = -1;
       try {
           car = System.in.read();
           while ((car > 0) && (car != newLine.charAt(0))) {
               buffer.append((char)car);
               car = System.in.read();
           }
           if (car == newLine.charAt(0))
           System.in.skip(newLine.length() - 1);
       } catch (java.io.IOException e) { return (null);}
       if ((car < 0) && (buffer.length() == 0)) return (null);
       return (buffer.toString()).trim();
   }

}
